package com.guochu.sys.controller;


import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.guochu.common.ResultMap;
import com.guochu.common.annotation.CloseAuth;
import com.guochu.sys.entity.SendSmsVO;
import com.guochu.utils.RandomUtils;
import com.guochu.utils.redis.RedisUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 手机短信管理
 */
@RestController
@RequestMapping("sys/sms")
public class SmsController {

    @Resource
    RedisUtil redis;

    /**
     * 发送短信验证码，用于验证码登录
     */
    @CloseAuth(true)
    @RequestMapping(value = "send", method = RequestMethod.POST)
    public ResultMap send(@RequestBody SendSmsVO sendSmsVO) throws Exception {
        String yanzhengma = RandomUtils.getRandomCode(1, 6);
        // 验证码存redis
        redis.set("smsVer_" + sendSmsVO.getPhoneNumber(), yanzhengma, 300, TimeUnit.SECONDS);
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5t6r8KgP6RwsRr9CKqCY", "d83Fs2ZLT9FEk5LQyy8xnq7DFl5iZb");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(sendSmsVO.getPhoneNumber())
                .setSignName("江苏工匠无忧科技有限公司")
                .setTemplateCode("SMS_227705205")
                .setTemplateParam("{code:'" + yanzhengma + "'}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sn = client.sendSms(sendSmsRequest);
        if (sn.getBody().getCode().equals("OK")) {
            return ResultMap.success();
        } else {
            return ResultMap.error("短信发送失败");
        }
    }

    /**
     * 接单通知，工人消息提醒
     */
    @CloseAuth(true)
    @RequestMapping(value = "smsJieYonghu", method = RequestMethod.POST)
    public ResultMap smsJieYonghu(@RequestBody SendSmsVO sendSmsVO) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5t6r8KgP6RwsRr9CKqCY", "d83Fs2ZLT9FEk5LQyy8xnq7DFl5iZb");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(sendSmsVO.getPhoneNumber())
                .setSignName("江苏工匠无忧科技有限公司")
                .setTemplateCode("SMS_237060594")
                .setTemplateParam("{}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sn = client.sendSms(sendSmsRequest);
        if (sn.getBody().getCode().equals("OK")) {
            return ResultMap.success();
        } else {
            return ResultMap.error("短信发送失败");
        }
    }

    /**
     * 接单通知，招工消息提醒
     */
    @CloseAuth(true)
    @RequestMapping(value = "smsJieQiye", method = RequestMethod.POST)
    public ResultMap smsJieQiye(@RequestBody SendSmsVO sendSmsVO) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5t6r8KgP6RwsRr9CKqCY", "d83Fs2ZLT9FEk5LQyy8xnq7DFl5iZb");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(sendSmsVO.getPhoneNumber())
                .setSignName("江苏工匠无忧科技有限公司")
                .setTemplateCode("SMS_237055645")
                .setTemplateParam("{number:'" + sendSmsVO.getContent() + "'}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sn = client.sendSms(sendSmsRequest);
        if (sn.getBody().getCode().equals("OK")) {
            return ResultMap.success();
        } else {
            return ResultMap.error("短信发送失败");
        }
    }

    /**
     * 取消订单
     */
    @CloseAuth(true)
    @RequestMapping(value = "smsQuxiao", method = RequestMethod.POST)
    public ResultMap smsQuxiao(@RequestBody SendSmsVO sendSmsVO) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5t6r8KgP6RwsRr9CKqCY", "d83Fs2ZLT9FEk5LQyy8xnq7DFl5iZb");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(sendSmsVO.getPhoneNumber())
                .setSignName("江苏工匠无忧科技有限公司")
                .setTemplateCode("SMS_237065989")
                .setTemplateParam("{}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sn = client.sendSms(sendSmsRequest);
        if (sn.getBody().getCode().equals("OK")) {
            return ResultMap.success();
        } else {
            return ResultMap.error("短信发送失败");
        }
    }

    public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

}